import { Toast } from "antd-mobile"
import { reqcartdelete, reqcartedit, reqcartlist } from "../../http/api"

const initState = {
    list: [],
    pre: "http://localhost:1000",
    //全选
    allChecked: false,
    //节流
    isshow: true,
    //总价
    allprice: 0,
}

const types = {
    CHANGE_LIST: "CHANGE_LIST",
    //修改全选
    CHANGE_ALLCHECKED: "CHANGE_ALLCHECKED",
    //
    CHANG_ISSHOW: "CHANGE_ISSHOW",
    //全选
    ALLPRICE: "ALLPRICE"

}
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_LIST:
            return {
                ...state,
                list: [...action.list]
            }
        //全选
        case types.CHANGE_ALLCHECKED:
            return {
                ...state,
                allChecked: action.allChecked
            }
        //
        case types.CHANG_ISSHOW:
            return {
                ...state,
                isshow: action.isshow
            }
        //ZONGJIE
        case types.ALLPRICE:
            return {
                ...state,
                allprice: action.allprice
            }
        default:
            return state
    }
}
export const actions = {
    //修改list
    changeList: list => ({ type: types.CHANGE_LIST, list }),
    //修改全选
    changeAllChecked: allChecked => ({ type: types.CHANGE_ALLCHECKED, allChecked }),
    //修改一个
    changeChecked: checked => ({ type: types.CHANGE_CHECKED, checked }),
    //
    chagnIsshow: isshow => ({ type: types.CHANG_ISSHOW, isshow }),
    //总价
    changeAllprice: allprice => ({ type: types.ALLPRICE, allprice }),

    //获取购物车列表
    reqList: () => (dispatch, getState) => {
        //请求list
        let uid = JSON.parse(sessionStorage.getItem("info")).uid
        reqcartlist({ uid }).then(res => {
            if (res.data.code == 200) {
                let arr = res.data.list[1].content?res.data.list[1].content:[]
                // console.log(arr);
                arr.forEach(item=>{
                    let obj = getList(getState()).find((i)=>i.id===item.id)
        
                    item.checked = obj?obj.checked:false
                })
                dispatch(actions.changeList(arr))

                // res.data.list[1].content.forEach(item => {
                //     item.checked = false
                // });
                // dispatch(actions.changeList(res.data.list[1].content))

            }
            dispatch(actions.chagnIsshow(true))
            //加减不取消checked
            //循环list
            // arr.forEach(item=>{
            //     //查找id相同的
            //     let obj = arr.find((i)=>i.id===item.id)
            //     console.log(obj.checked);
            //     //给页面的checked渲染
            //     item.checked = obj?obj.checked:false
            // })
            // dispatch(actions.changeList(arr))

        })
    },
    //点了全选
    reqAllChecked: (e) => (dispatch, getState) => {
        dispatch(actions.changeAllChecked(e))
        let list = getList(getState())
        let allChecked = getAllChecked(getState())
        list.forEach((item) => {
            item.checked = allChecked
        });
        dispatch(actions.changeList(list))
        let allPricee = getList(getState())
        console.log(allPricee);
        console.log(allPricee.reduce((val, item) => (item.checked ? val + item.price * item.num : val),));
        dispatch(actions.changeAllprice((val, item) => (item.checked ? val + item.price * item.num : val),))
    },
    //点了一个
    reqChecked: (e, index) => (dispatch, getState) => {
        let arr = getList(getState())
        arr[index].checked = e
        dispatch(actions.changeList(arr))
        //全选
        dispatch(actions.changeAllChecked(arr.every(item => item.checked)))

    },
    //删除
    reqcartdelete: (e, index) => (dispatch, getState) => {
        reqcartdelete({ id: e.list[index].id }).then(res => {
            if (res.data.code == 200) {
                Toast.show({
                    content: res.data.msg
                })
                //刷新
                dispatch(actions.reqList())
            }
        })
    },
    // num+-
    reqcartedit: (e, index, i) => (dispatch, getState) => {
        //判断能不能发
        if (!getIsshow(getState())) {
            return;
        }
        console.log(e.list[index].num);
        //判断是否小于等于一
        if (Number(e.list[index].num) <= 1 && i == 1) {
            return
        }
        let authorization = JSON.parse(sessionStorage.getItem("info")).token
        reqcartedit({ id: e.list[index].id, type: i, authorization }).then(res => {
            // dispatch(initState(isshow = true))
            if (res.data.code == 200) {
                dispatch(actions.reqList())
            }
        })
        // console.log(e.allprice);
        // dispatch(actions,changeAllprice())

        dispatch(actions.chagnIsshow(false))



        // let arr = getList(getState())
        // console.log(arr);
        // Number(arr[index-1]).num = e[index].num+1
        // dispatch(actions.changeList(arr))
    },
}

//导出
export const getList = state => state.shop.list
export const getPre = state => state.shop.pre
export const getAllChecked = state => state.shop.allChecked
export const getIsshow = state => state.shop.isshow
export const getAllPrice = state => state.shop.allprice

//导出reducer
export default reducer